package com.tledu.mybatis01;

import com.tledu.mybatis01.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/mybatis")
public class HelloServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //作业都在测试用例中

        //加载配置文件
        String config = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);
        //获取session工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取session
        SqlSession session = sqlSessionFactory.openSession();
        //调用xml中的sql
        Student student = new Student();
        student.setAge(10);
        student.setBirthday(new Date());
        student.setPassword("123");
        student.setUsername("antg");
        student.setNickname("antg");
        //执行sql
        session.insert("Student.add",student);
        //提交
        session.commit();
    }
}